<?php 
include_once "function_xml_basic.php";

function GetXmlBridgeList()
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Type='bridge']";

	$ret_array = array();
	$results = GetAttribute($query_string);
	$i = 0;
	foreach ($results as $result)
	{
		$ret_array[$i]['Name'] = $result->getAttribute('Name');
                $ret_array[$i]['Ports'] = $result->getAttribute('Ports');
                $ret_array[$i]['Stp'] = $result->getAttribute('Stp');
		
		$query_string2 ="/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $ret_array[$i]['Name']. 
				"' and @Type='bridge']/ADDRESSES/ADDRESS";
		
		$ret_address = GetAttribute($query_string2);
		$k = 0;
		foreach ($ret_address as $address)
		{
			$ret_array[$i]['Ip'][$k] = $address->getAttribute('Ip');		
			$ret_array[$i]['Mask'][$k] = $address->getAttribute('Mask');
			$k ++;
		}

                $i ++;
	}
	return $ret_array;
}

#$ret_array = GetXmlBridgeList();
#foreach ($ret_array as $ret)
#{
#	print $ret['Name'];
#	foreach ($ret['Ip'] as $tmp)
#		print $tmp;
#}

function GetXmlBridgeInfoByName($birdge_name)
{
	$ret_array = GetXmlBridgeList();
	
	foreach ($ret_array as $ret)
	{
		if ($ret['Name'] == $birdge_name)
			return $ret;
	}
	return NULL;
}

function DelXmlNetDevOneBridge($name)
{
	FlushSysAllBridgeConfig();
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $name. "']";
	DelSpecialNode($query_string);
	ApplySysAllBridgeDevInfo();
}

function DelXmlNetDevAllBridge()
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Type='bridge']";
	DelSpecialNode($query_string);
	FlushSysAllBridgeConfig();

}

function AppendXmlNetDevOneBridge($Valu_array)
{
	FlushSysAllBridgeConfig();
	$node_name = "INTERFACE";
	$query_string = "/MINI/NETWORK/INTERFACES";
	$Valu_array['Type'] = "bridge";
	
	$temp_array ['Ip'] = $Valu_array['Ip'];
	$temp_array ['Mask'] = $Valu_array['Mask'];
	unset($Valu_array['Ip']);
	unset($Valu_array['Mask']);
	AppendAllAttrOfNode($query_string, $node_name, $Valu_array);

	$query_string .= "/". $node_name. "[last()]";
	$node_name = "ADDRESSES";
	$null_array = array ();
	AppendAllAttrOfNode($query_string, $node_name, $null_array);
	
	$query_string .= "/". $node_name;
	$node_name = "ADDRESS";
	AppendAllAttrOfNode($query_string, $node_name, $temp_array);
	ApplySysAllBridgeDevInfo();
}

function EditXmlNetDevBridge($in_array)
{
	FlushSysAllBridgeConfig();
	$dev = $in_array['Name'];
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']";
	$edit_key['IpMigration'] = $in_array['IpMigration'];
	$edit_key['Stp'] = $in_array['Stp'];
	$edit_key['Ports'] = $in_array['Ports'];
	EditAllAttrOfNode($query_string, $edit_key);

	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']/ADDRESSES/ADDRESS";
	$edit_key2['Ip'] = $in_array['Ip'];
	$edit_key2['Mask'] = $in_array['Mask'];
	EditAllAttrOfNode($query_string, $edit_key2);
	ApplySysAllBridgeDevInfo();
}

function GetSpecialBirdgeDevList($list_string)
{
	$ret_array = explode(",", $list_string);
	if ($ret_array[count($ret_array) - 1] == '')
		array_pop($ret_array);
	#foreach ($ret_array as $i)
	#{
	#	print "[". $i. "]\n"; 
	#}
	return $ret_array;
}

function GetBridgeDevListFromSys()
{
	$command_string = "/usr/local/sbin/brctl show | grep -E 'no|yes' | awk '{print $1}'";
	$ret_string = shell_exec($command_string);
	
	$ret_array = explode("\n", $ret_string);
	
	#foreach ($ret_array as $ret)
	#{
	#	print "[". $ret. "]\n";
	#}
	return $ret_array;
}

function GetBridgeDevInfoFromSys($br)
{
	$command_string = "/usr/local/sbin/brctl showstp ". $br. " | grep eth[0-9] | awk '{print $1}'";
	$ret_string = shell_exec($command_string);
	
	$ret_array = explode("\n", $ret_string);
	
	#foreach ($ret_array as $ret)
	#{
	#	print "[". $ret. "]\n";
	#}
	return $ret_array;
}

include_once "function_misc.php";

function ApplySysAllBridgeDevInfo()
{
	$command = "";
	$command_brctl = "/usr/local/sbin/brctl ";
	$command_ifconfig = "/sbin/ifconfig ";

	$ret_br_list = GetXmlBridgeList();	


	foreach($ret_br_list as $br)
	{
		$ip_addr = "";
		foreach ($br['Ip'] as $ip)
		{
			$ip_addr .= $ip;
		}
		$ip_mask = "";
		foreach ($br['Mask'] as $mask)
		{
			$ip_mask .= $mask;
		}

		$ports_string = DelEndComma($br['Ports']);
		$eth_list = explode(",", $ports_string);
		foreach ($eth_list as $eth)
		{
			if ($eth == "")
				continue;
			$command .= $command_ifconfig. " ". $eth. " 0 ;";
		}
		$ports = str_replace(","," ",$ports_string);
		
		$stp = "";
		if ($br['Stp'] == 1)
			$stp = " on ";
		else
			$stp = " off ";

		$command .= $command_brctl. " addbr ". $br['Name']. " ;";
		$command .= $command_brctl. " addif ". $br['Name']. " ". $ports. " ;";
		$command .= $command_brctl. " stp ". $br['Name']. $stp. " ;";
		$command .= $command_ifconfig. $br['Name']. " up ; ";
		$command .= $command_ifconfig. $br['Name']. " ". $ip_addr. " netmask ". $ip_mask. " ; ";
	}
	//print $command;
	$ret_string = shell_exec($command);
	return $ret_string;
}

function FlushSysAllBridgeConfig()
{
	$ret_array_br_list = GetBridgeDevListFromSys();

	$command = "";
	$command_brctl = "/usr/local/sbin/brctl ";
	$command_ifconfig = "/sbin/ifconfig ";

	foreach ($ret_array_br_list as $br)
	{
		if ($br == "")
			continue;

		$ret_br_info = GetBridgeDevInfoFromSys($br);	
		$ports_string = "";
		foreach ($ret_br_info as $port)
		{
			$ports_string .= " ". $port. " ";	
		}
		$command .= $command_brctl. " delif ". $br. " ". $ports_string. ";";
		$command .= $command_ifconfig. $br. " 0 ;";
		$command .= $command_ifconfig. $br. " down ;";
		$command .= $command_brctl. " delbr ". $br. ";";
	}
	//print $command;
	$ret_string = shell_exec($command);
	return $ret_string;
}

?>
